home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
ms_dos
/
txf
/
src
/
txfex_p.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-08
|
4KB
|
174 lines
/***************
*
* g:\exe\txf\src\txfex_p.c
*/
#include "txf.h"
void exchange(char *oldfile,char *newfile)
{ /* old→new */
FILE *tmpin, *tmpout;
int i = 0, j, clum = 0, knj = 0;
int sameflg = -1, diff = 0, maxlen = 0, chr = NUL, chrattr = CT_ANK;
unsigned long int extime = 0;
char *form = NULL, *tmpbuf;
for (i = 0; i < exflg; i++) {
form = malloc(strlen(exold[i]) + 1);
if (form == NULL) {
errexit("cannot use enough memory(old)");
}
strcpy(form, exold[i]);
/* free(exold[i]); */
exold[i] = ftrans(form);
free(form);
if (maxlen < strlen(exold[i])) {
maxlen = strlen(exold[i]);
}
exattr[i] = malloc(strlen(exold[i]) + 1);
if (exattr[i] == NULL) {
errexit("cannot use enough memory(ex_attr)");
}
for (j = 0; *(exold[i] + j) != NUL; j++) {
chrattr = chkctype(*(exold[i] + j), chrattr);
*(exattr[i] + j) = chrattr;
}
if (exnew[i] != NULL) {
form = malloc(strlen(exnew[i]) + 1);
if (form == NULL) {
errexit("cannot use enough memory(new)");
}
strcpy(form, exnew[i]);
/* free(exnew[i]); */
exnew[i] = ftrans(form);
free(form);
}
}
tmpbuf = calloc(maxlen + 1, 1);
if (*oldfile != NUL) {
tmpin=fopen(oldfile,"rb");
}
else {
tmpin=stdin;
}
tmpout = fopen(newfile, "wb");
if (viewmode > 2) {
fprintf(stderr,"Info:readfile=%s,(%d)/writefile=%s,(%d)\n",
oldfile, tmpin, newfile, tmpout);
}
if ((tmpin == NULL) || (tmpout == NULL)) {
errexit("cannot open TMP/input file(exchange-P)\n");
}
for (j = 0; j < exflg; j++) {
exptr[j] = exold[j];
exattrptr[j] = exattr[j];
}
i = 0;
chrattr = CT_ANK;
while (chr != EOF) {
sameflg =- 1;
while (sameflg == -1) {
chr = getc(tmpin);
if (chr == RET) {
clum = 0;
quote = 0;
}
else {
clum++;
}
if (knj > 0) {
if ((clum == 2) && (jstrchr(kq, chr | knj<<8) != NULL)) {
quote = 1;
}
}
else {
if ((clum == 1) && (jstrchr(kq, (chr & 0x0ff)) != NULL)) {
quote=1;
}
}
chrattr = chkctype(chr, chrattr);
if (chrattr == CT_KJ1) {
knj = chr;
}
else {
knj = 0;
}
diff = 0;
for (j = 0; j < exflg; j++) {
if ((chr == *(exptr[j])) && (chrattr == *(exattrptr[j]))
&& !(quote & quoteflg)) {
exptr[j] += 1;
exattrptr[j] += 1;
if (*exptr[j] == NUL) {
sameflg = j;
}
}
else {
if (exold[j] != exptr[j]) {
exptr[j] = exold[j];
exattrptr[j] = exattr[j];
}
diff ++;
}
}
if ((sameflg == -1) && (chr != EOF)) {
if (diff < exflg) {
*(tmpbuf+i) = chr;
i++;
*(tmpbuf+i) = NUL;
}
else {
if (i > 0) {
fwrite(tmpbuf,1,i,tmpout);
}
putc(chr, tmpout);
i = 0;
}
}
if (chr == EOF) break;
}
if (sameflg != -1) {
if (exnew[sameflg] != NUL) {
fwrite(exnew[sameflg], 1, strlen(exnew[sameflg]), tmpout);
}
exptr[sameflg] = exold[sameflg];
exattrptr[sameflg] = exattr[sameflg];
i = 0;
extime++;
if (viewmode > 1) fprintf(stderr, "%ld%c", extime, 0x0d);
}
else {
fwrite(tmpbuf,1,i,tmpout);
}
i=0;
}
if (viewmode>0) {
fprintf(stderr, "%ldtime(s) exchanged!\n", extime);
}
fclose(tmpin);
fclose(tmpout);
free(tmpbuf);
for (j = 0; j < exflg; j++) {
free(exattr[j]);
}
}
void exchangedriver()
{
char *tmpinname, *tmpoutname;
tmpinname = ((tmpinfile == -1) ? inputfile : tfile[tmpinfile]);
tmpoutname = tfile[((tmpinfile > 0) ? 0 : 1)];
if (viewmode > 0)
fprintf(stderr, "TXF Super exchange module.(Parallel) Ver2.01b\n");
if (exold[0] != NULL) {
exchange(tmpinname, tmpoutname);
}
tmpinfile = ((tmpinfile > 0) ? 0 : 1);
tmpinname = tfile[tmpinfile];
tmpoutname = tfile[1 - tmpinfile];
}